using System;
using System.IO;
using System.Text;
namespace APP.HELPER.Utils.Net
{
	public class MailEncoder
	{
		public static string GetBase64String(string filePath)
		{
			string result;
			if (File.Exists(filePath))
			{
				FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
				byte[] array = new byte[fileStream.Length];
				int length = fileStream.Read(array, 0, array.Length);
				string text = Convert.ToBase64String(array, 0, length);
				fileStream.Close();
				StringBuilder stringBuilder = new StringBuilder();
				int num = 76;
				for (int i = 0; i < text.Length; i += num)
				{
					if (text.Length - (i + num) < 0)
					{
						num = text.Length - i;
					}
					stringBuilder.Append(text.Substring(i, num));
					stringBuilder.Append("\r\n");
				}
				result = stringBuilder.ToString();
			}
			else
			{
				result = null;
			}
			return result;
		}
		public static string ConvertToQP(string s, string charset)
		{
			string result;
			if (s == null)
			{
				result = null;
			}
			else
			{
				if (charset == null)
				{
					charset = "ISO-8859-1";
				}
				char[] array = Environment.NewLine.ToCharArray();
				char[] array2 = s.ToCharArray();
				StringBuilder stringBuilder = new StringBuilder();
				StringBuilder stringBuilder2 = new StringBuilder();
				bool flag = false;
				Encoding encoding = Encoding.GetEncoding(charset);
				for (int i = 0; i < s.Length; i++)
				{
					char c = array2[i];
					if (c == array[0] && i <= s.Length - array.Length)
					{
						if (s.Substring(i, array.Length) == Environment.NewLine)
						{
							if (stringBuilder2.Length > 0)
							{
								if (array2[i - 1] == ' ')
								{
									stringBuilder2.Remove(stringBuilder2.Length - 1, 1);
									stringBuilder2.Append("=20");
								}
								else
								{
									if (array2[i - 1] == '\t')
									{
										stringBuilder2.Remove(stringBuilder2.Length - 1, 1);
										stringBuilder2.Append("=09");
									}
								}
							}
							flag = true;
							i += array.Length - 1;
						}
						else
						{
							stringBuilder2.Append("=0" + Convert.ToString((byte)c, 16).ToUpper());
						}
					}
					else
					{
						if (c > '~' || (c < ' ' && c != '\t') || c == '=')
						{
							byte[] array3 = new byte[10];
							int bytes = encoding.GetBytes(string.Concat(c), 0, 1, array3, 0);
							for (int j = 0; j < bytes; j++)
							{
								if (array3[j] < 16)
								{
									stringBuilder2.Append("=0" + Convert.ToString(array3[j], 16).ToUpper());
								}
								else
								{
									stringBuilder2.Append("=" + Convert.ToString(array3[j], 16).ToUpper());
								}
							}
						}
						else
						{
							stringBuilder2.Append(c);
						}
					}
					if (stringBuilder2.Length >= 76)
					{
						string text = stringBuilder2.ToString();
						int num = text.LastIndexOf("=");
						if (num < 70)
						{
							num = 74;
						}
						stringBuilder.Append(text.Substring(0, num) + "=\r\n");
						stringBuilder2 = new StringBuilder(text.Substring(num));
					}
					if (flag)
					{
						stringBuilder.Append(stringBuilder2 + "\r\n");
						stringBuilder2 = new StringBuilder();
						flag = false;
					}
				}
				stringBuilder.Append(stringBuilder2.ToString());
				result = stringBuilder.ToString();
			}
			return result;
		}
	}
}
